home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1459.dms / var1459.adf / LowLevelGraphics / Example9.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  9KB  |  330 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example9.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example shows how to flood fill a figure, and how to draw filled */
  21. /* rectangles (both solid as well as filled with single and multi        */
  22. /* coloured patterns).                                                   */
  23.  
  24.  
  25. #include <intuition/intuition.h>
  26. #include <graphics/gfxbase.h>
  27. #include <graphics/gfxmacros.h>
  28.  
  29.  
  30. /* NOTE! We must include the file "gfxmacros.h" inorder to be able to */
  31. /* use the functions (macros) SetOPen() and SetAfPt().                */
  32.  
  33.  
  34. #define WIDTH  320 /* 320 pixels wide (low resolution)                */
  35. #define HEIGHT 200 /* 200 lines high (non interlaced NTSC display)    */ 
  36. #define DEPTH    2 /* 2 BitPlanes should be used, gives four colours. */
  37. #define COLOURS  4 /* 2^2 = 4                                         */
  38.  
  39.  
  40. #define OUTLINE_MODE 0 /* Fill until we find same colour as Outline Pen. */
  41. #define COLOUR_MODE  1 /* Fill until we find another colour.             */
  42.  
  43.  
  44. struct IntuitionBase *IntuitionBase;
  45. struct GfxBase *GfxBase;
  46.  
  47.  
  48. struct View my_view;
  49. struct View *my_old_view;
  50. struct ViewPort my_view_port;
  51. struct RasInfo my_ras_info;
  52. struct BitMap my_bit_map;
  53. struct RastPort my_rast_port;
  54.  
  55.  
  56. UWORD my_color_table[] =
  57. {
  58.   0x000, /* Colour 0, Black */
  59.   0xF00, /* Colour 1, Red   */
  60.   0x0F0, /* Colour 2, Green */
  61.   0x00F  /* Colour 3, Blue  */
  62. };
  63.  
  64.  
  65. /* The coordinates (25) for the PolyDraw() function: */
  66. WORD coordinates[] =
  67. {
  68.     0,   0,
  69.   120,   0,
  70.   120,  40,
  71.   180,  40,
  72.   180,   0,
  73.   300,   0,
  74.   300,  20,
  75.   200,  20,
  76.   200,  60,
  77.   160,  60,
  78.   160, 100,
  79.   280, 100,
  80.   280,  40,
  81.   300,  40,
  82.   300, 120,
  83.     0, 120,
  84.     0,  40,
  85.    20,  40,
  86.    20, 100,
  87.   140, 100,
  88.   140,  60,
  89.   100,  60,
  90.   100,  20,
  91.     0,  20,
  92.     0,   0
  93. };
  94.  
  95.  
  96. /* A heart (1 BitPlane):                                               */
  97. /* An area pattern is always 16 bits wide, and the hight is some power */
  98. /* of two (1, 2, 4, 8, 16, 32, and so on ).                            */
  99. UWORD pattern[] =
  100. {
  101.   0x38E0, /* 0011 1000 1110 0000 */
  102.   0x7DF0, /* 0111 1101 1111 0000 */
  103.   0xFFF8, /* 1111 1111 1111 1000 */
  104.   0xFFF8, /* 1111 1111 1111 1000 */
  105.   0xFFF8, /* 1111 1111 1111 1000 */
  106.   0x7FF0, /* 0111 1111 1111 0000 */
  107.   0x3FE0, /* 0011 1111 1110 0000 */
  108.   0x1FC0, /* 0001 1111 1100 0000 */
  109.   0x0F80, /* 0000 1111 1000 0000 */
  110.   0x0700, /* 0000 0111 0000 0000 */
  111.   0x0200, /* 0000 0010 0000 0000 */
  112.  
  113.   0x0000, /* 0000 0000 0000 0000 */
  114.   0x0000, /* 0000 0000 0000 0000 */
  115.   0x0000, /* 0000 0000 0000 0000 */
  116.   0x0000, /* 0000 0000 0000 0000 */
  117.   0x0000, /* 0000 0000 0000 0000 */
  118. };
  119.  
  120.  
  121. /* A four-coloured pattern: (Black, red, green and blue lines) */
  122. UWORD coloured_pattern[][] =
  123. {
  124.   {
  125.     0x00FF, /* BitPlane 0 */
  126.     0xFF00,
  127.     0x00FF,
  128.     0xFF00
  129.   },
  130.   {
  131.     0x00FF, /* BitPlane 1 */
  132.     0x00FF,
  133.     0xFF00,
  134.     0xFF00
  135.   }
  136. };
  137.  
  138.  
  139. void clean_up();
  140. void main();
  141.  
  142.  
  143. void main()
  144. {
  145.   UWORD *pointer;
  146.   int loop;
  147.   
  148.  
  149.   /* Open the Intuition library: */
  150.   IntuitionBase = (struct IntuitionBase *)
  151.     OpenLibrary( "intuition.library", 0 );
  152.   if( !IntuitionBase )
  153.     clean_up( "Could NOT open the Intuition library!" );
  154.  
  155.   /* Open the Graphics library: */
  156.   GfxBase = (struct GfxBase *)
  157.     OpenLibrary( "graphics.library", 0 );
  158.   if( !GfxBase )
  159.     clean_up( "Could NOT open the Graphics library!" );
  160.  
  161.  
  162.   /* Save the current View, so we can restore it later: */
  163.   my_old_view = GfxBase->ActiView;
  164.  
  165.  
  166.   /* 1. Prepare the View structure, and give it a pointer to */
  167.   /*    the first ViewPort:                                  */
  168.   InitView( &my_view );
  169.   my_view.ViewPort = &my_view_port;
  170.  
  171.  
  172.   /* 2. Prepare the ViewPort structure, and set some important values: */
  173.   InitVPort( &my_view_port );
  174.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  175.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  176.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  177.   my_view_port.Modes = NULL;           /* Low resolution.               */
  178.  
  179.  
  180.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  181.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  182.   if( my_view_port.ColorMap == NULL )
  183.     clean_up( "Could NOT get a ColorMap!" );
  184.  
  185.   /* Get a pointer to the colour map: */
  186.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  187.  
  188.   /* Set the colours: */
  189.   for( loop = 0; loop < COLOURS; loop++ )
  190.     *pointer++ = my_color_table[ loop ];
  191.  
  192.  
  193.   /* 4. Prepare the BitMap: */
  194.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  195.  
  196.   /* Allocate memory for the Raster: */ 
  197.   for( loop = 0; loop < DEPTH; loop++ )
  198.   {
  199.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  200.     if( my_bit_map.Planes[ loop ] == NULL )
  201.       clean_up( "Could NOT allocate enough memory for the raster!" );
  202.  
  203.     /* Clear the display memory with help of the Blitter: */
  204.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  205.   }
  206.  
  207.   
  208.   /* 5. Prepare the RasInfo structure: */
  209.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  210.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  211.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  212.                                     /* of the display.                   */
  213.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  214.                                     /* RasInfo structure is necessary.   */
  215.  
  216.   /* 6. Create the display: */
  217.   MakeVPort( &my_view, &my_view_port );
  218.   MrgCop( &my_view );
  219.  
  220.  
  221.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  222.   InitRastPort( &my_rast_port );
  223.   my_rast_port.BitMap = &my_bit_map;
  224.   
  225.  
  226.   /* 8. Show the new View: */
  227.   LoadView( &my_view );
  228.  
  229.  
  230.  
  231.   SetDrMd( &my_rast_port, JAM2 ); /* Use Fg and Bg Pen. */
  232.   SetAPen( &my_rast_port, 3 );    /* FgPen:  Blue       */
  233.   SetBPen( &my_rast_port, 2 );    /* BgPen:  Green      */
  234.   SetOPen( &my_rast_port, 3 );    /* BgPen:  Blue       */
  235.  
  236.  
  237.   /* Draw a funny figure in blue colour: */
  238.   Move( &my_rast_port, 0, 0 );
  239.   PolyDraw( &my_rast_port, 25, coordinates );
  240.  
  241.  
  242.   /* Wait 5 seconds: */
  243.   Delay( 50 * 5 );
  244.  
  245.  
  246.   /* Change FgPen colour to red, and fill the figure: */
  247.   SetAPen( &my_rast_port, 1 );    /* FgPen: Red       */
  248.   Flood( &my_rast_port, OUTLINE_MODE, 10, 10 );
  249.  
  250.  
  251.   /* Wait 5 seconds: */
  252.   Delay( 50 * 5 );
  253.   
  254.  
  255.   /* Draw a filled rectangle at the bottom of the display: */
  256.   RectFill( &my_rast_port, 0, 150, 150, 190 );
  257.  
  258.  
  259.   /* Wait 5 seconds: */
  260.   Delay( 50 * 5 );
  261.  
  262.  
  263.   /* Set the are pattern. We will now draw a rectangle filled with a */
  264.   /* lot of  hearts. (The pattern is 16 lines tall which is 2 to the  */
  265.   /* power of 4.)                                                    */
  266.   SetAfPt( &my_rast_port, (USHORT *) pattern, 4);
  267.  
  268.   /* Draw a rectangle filled with hearts at the bottom of the display: */
  269.   RectFill( &my_rast_port, 150, 150, 300, 190 );
  270.  
  271.  
  272.   /* Wait 5 seconds: */
  273.   Delay( 50 * 5 );
  274.  
  275.  
  276.   /* Prepare to fill with a coloured pattern: */
  277.   /* Drawmode JAM2, FgPen colour 255, BgPen 0 */
  278.   SetDrMd( &my_rast_port, JAM2 );
  279.   SetAPen( &my_rast_port, 255 );
  280.   SetBPen( &my_rast_port, 0 );
  281.   SetAfPt( &my_rast_port, (USHORT *) coloured_pattern, -2);
  282.   /* 4 lines = 2^2 -> 2 : Multicolour: -2 */
  283.  
  284.   /* Draw a rectangle filled with four colours: */
  285.   RectFill( &my_rast_port, 0, 150, 300, 190 );
  286.  
  287.  
  288.   /* Wait 5 seconds: */
  289.   Delay( 50 * 5 );
  290.  
  291.  
  292.  
  293.   /* 9. Restore the old View: */
  294.   LoadView( my_old_view );
  295.  
  296.  
  297.   /* Free all allocated resources and leave. */
  298.   clean_up( "THE END" );
  299. }
  300.  
  301.  
  302. /* Returns all allocated resources: */
  303. void clean_up( message )
  304. STRPTR message;
  305. {
  306.   int loop;
  307.  
  308.   /* Free automatically allocated display structures: */
  309.   FreeVPortCopLists( &my_view_port );
  310.   FreeCprList( my_view.LOFCprList );
  311.   
  312.   /* Deallocate the display memory, BitPlane for BitPlane: */
  313.   for( loop = 0; loop < DEPTH; loop++ )
  314.     if( my_bit_map.Planes[ loop ] )
  315.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  316.  
  317.   /* Deallocate the ColorMap: */
  318.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  319.  
  320.   /* Close the Graphics library: */
  321.   if( GfxBase ) CloseLibrary( GfxBase );
  322.  
  323.   /* Close the Intuition library: */
  324.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  325.  
  326.   /* Print the message and leave: */
  327.   printf( "%s\n", message ); 
  328.   exit();
  329. }
  330.